/*实习考核相关js*/
//获取html代码中的试题,并拼接成数组
function GetTestData(list) {
	var data = list;
	var dataArr = new Array();
	var danArr = new Array();
	var duoArr = new Array();
	var panArr = new Array();
	var shituArr = new Array();
	var paixuArr = new Array();
	var shipinArr = new Array();
	var lunshuArr = new Array();
	var anliArr = new Array();
	if (data != null && data != '') {
		if (data.dan != undefined && data.dan.length > 0 && data.dan != '' && data.dan != null) {
			for (var i = 0; i < data.dan.length; i++) {
				var weeklyData = data.dan[i];
				danArr.push(weeklyData);
			}
		}
		if (data.duo != undefined && data.duo.length > 0 && data.duo != '' && data.duo != null) {
			for (var i = 0; i < data.duo.length; i++) {
				var duoData = data.duo[i];
				duoArr.push(duoData);
			}
		}
		if (data.pan != undefined && data.pan.length > 0 && data.pan != '' && data.pan != null) {
			for (var i = 0; i < data.pan.length; i++) {
				var panData = data.pan[i];
				panArr.push(panData);
			}
		}
		if (data.lunshu != undefined && data.lunshu.length > 0 && data.lunshu != '' && data.lunshu != null) {
			for (var i = 0; i < data.lunshu.length; i++) {
				var lunshuData = data.lunshu[i];
				lunshuArr.push(lunshuData);
			}
		}
		if (data.shitu != undefined && data.shitu.length > 0 && data.shitu != '' && data.shitu != null) {
			for (var i = 0; i < data.shitu.length; i++) {
				var shituData = data.shitu[i];
				shituArr.push(shituData);
			}
		}
		if (data.paixu != undefined && data.paixu.length > 0 && data.paixu != '' && data.paixu != null) {
			for (var i = 0; i < data.paixu.length; i++) {
				var paixuData = data.paixu[i];
				paixuArr.push(paixuData);
			}
		}
		if (data.shipin != undefined && data.shipin.length > 0 && data.shipin != '' && data.shipin != null) {
			for (var i = 0; i < data.shipin.length; i++) {
				var shipinData = data.shipin[i];
				shipinArr.push(shipinData);
			}
		}
		if (data.anli != undefined && data.anli.length > 0 && data.anli != '' && data.anli != null) {
			for (var i = 0; i < data.anli.length; i++) {
				var anliData = data.anli[i];
				anliArr.push(anliData);
			}
		}
		dataArr = dataArr.concat(danArr, duoArr, panArr, lunshuArr, shituArr, paixuArr, shipinArr, anliArr);
		return dataArr;
	}
}



//选择题
function panxuanzeReturnStr(i) {
	var num = (i + 1) % 2;
	switch(num) {
		case 0:
			return '错误'
			break;
		case 1:
			return '正确'
			break;
		default:
			return '暂无'
			break;
	}
}

//	生成答题卡题号
function popoverCardNum(questionLen) {
	for (var i = 0; i < questionLen; i++)
		$("#scroll-card").append('<span class="qusetion-num-normal">' + (i + 1) + '</span>')
}

//测验结果生成答题卡
function TestScorePopoverCardNum(questionLen) {
	for (var i = 0; i < questionLen; i++) {
		$("#scroll-card").append('<span class="qusetion-num-selected" >' + (i + 1) + '</span>')
	}
}

//选项是否有图片
function xuanzheImgIsNull(xuanzheStr) {

	if (!isEmpty(xuanzheStr)) {
		if (xuanzheStr.toString().indexOf("img") > 0) {//题目选项中带有图片
			var ImgArr = RegularGetImgSrcArr(xuanzheStr);
			var words = "";
			if (!isEmpty(htmlspecialchars_decode(xuanzheStr))) {
				words = htmlspecialchars_decode(xuanzheStr);
			} else {
				words = '<br>';
			}
			if (ImgArr.length > 1) {//图片张数超过1张
				var ImgStr1 = "";
				for (var i = 0; i < ImgArr.length; i++) {
					var imgstr2 = PicDomainName(ImgArr[i], 120);
					var imgstr3 = '<img class="xuanzeImgDiv top_5" src="' + imgstr2 + '" imgUrl="' + ImgArr[i] + '"  alt="" onclick="showImg(this)">';
					ImgStr1 = ImgStr1 + imgstr3;
				}
				return words + ImgStr1;
			} else {
				var xuanzheImgstr = PicDomainName(ImgArr[0], 120);
				return words + '<img class="xuanzeImgDiv" src="' + xuanzheImgstr + '"  imgUrl="' + ImgArr[0] + '" alt="" onclick="showImg(this)">';
			}

		} else if (xuanzheStr.toString().indexOf("edui-video-resource") > 0 || xuanzheStr.toString().indexOf("video") > 0) {//图片中带有视频
			var VideoUrlArr = xuanzheStr.indexOf("edui-video-resource") > 0 ? RegularGetVideoUrlArr(xuanzheStr) : RegularGetVideotwoUrlArr(xuanzheStr);
			var words = "";
			if (!isEmpty(htmlspecialchars_decode(xuanzheStr))) {
				words = htmlspecialchars_decode(xuanzheStr);
			} else {
				words = '<br>';
			}
			if (VideoUrlArr.length > 1) {//图片张数超过1张
				var ImgStr1 = "";
				for (var i = 0; i < VideoUrlArr.length; i++) {
					var imgstr2 = VideoUrlArr[i];
					var imgstr3 = '<div class="option_video_div"  video-url="' + VideoUrlArr[i] + '" onclick="toPlayVideo(this)">' + '<img src="../../image/img_voide.png"/>' + '</div>';
					ImgStr1 = ImgStr1 + imgstr3;
				}
				return words + ImgStr1;
			} else {
				var questionsItemstr = VideoUrlArr[0];
				return words + '<div class="option_video_div"  video-url="' + VideoUrlArr[0] + '" onclick="toPlayVideo(this)">' + '<img src="../../image/img_voide.png"/>' + '</div>';
			}

		} else {
			return htmlspecialchars_decode(xuanzheStr);
		}
	} else {
		return '';
	}
}

//题目是否有图片

function questionsImgIsNull(questionsStr) {
	
	if (!isEmpty(questionsStr)) {
		if (questionsStr.indexOf("img") > 0) {
			var ImgArr = RegularGetImgSrcArr(questionsStr);
			var words = "";
			if (!isEmpty(htmlspecialchars_decode(questionsStr))) {
				words = htmlspecialchars_decode(questionsStr);
			} else {
				words = '<br>';
			}
			if (ImgArr.length > 1) {//图片张数超过1张
				var ImgStr1 = "";
				for (var i = 0; i < ImgArr.length; i++) {
					var imgstr2 = PicDomainName(ImgArr[i], 320);
					var imgstr3 = '<img class="questionItemImgDiv"  src="' + imgstr2 + '" imgUrl="' + ImgArr[i] + '"  alt="" onclick="showImg(this)">';
					ImgStr1 = ImgStr1 + imgstr3;
				}
				return words + ImgStr1;
			} else {
				var questionsItemImgstr = PicDomainName(ImgArr[0], 320);
				return words + '<img class="questionItemImgDiv"  src="' + questionsItemImgstr + '" imgUrl="' + ImgArr[0] + '"  alt="" onclick="showImg(this)">';
			}
		} else if (questionsStr.toString().indexOf("edui-video-resource") > 0 || questionsStr.toString().indexOf("video") > 0) {//

			var VideoUrlArr = questionsStr.indexOf("edui-video-resource") > 0 ? RegularGetVideoUrlArr(questionsStr) : RegularGetVideotwoUrlArr(questionsStr);
			var words = "";
			if (!isEmpty(htmlspecialchars_decode(questionsStr))) {
				words = htmlspecialchars_decode(questionsStr);
			} else {
				words = '<br>';
			}
			if (VideoUrlArr.length > 1) {//图片张数超过1张
				var ImgStr1 = "";
				for (var i = 0; i < VideoUrlArr.length; i++) {
					var imgstr2 = VideoUrlArr[i];
					var imgstr3 = '<div class="question_video_div"  video-url="' + VideoUrlArr[i] + '" onclick="toPlayVideo(this)">' + '<img src="../../image/img_voide.png"/>' + '</div>';
					ImgStr1 = ImgStr1 + imgstr3;
				}
				return words + ImgStr1;
			} else {
				var questionsItemstr = VideoUrlArr[0];
				return words + '<div class="question_video_div"  video-url="' + VideoUrlArr[0] + '" onclick="toPlayVideo(this)">' + '<img src="../../image/img_voide.png"/>' + '</div>';
			}
		} else {
			return htmlspecialchars_decode(questionsStr);
		}
	} else {
		return '';
	}
}

//正则获取img标签中的src地址
function RegularGetImgSrcArr(str) {
	var srcArr = new Array();
	var imgReg = /<img.*?(?:>|\/>)/gi;
	//匹配src属性
	var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
	var arr = str.match(imgReg);
	for (var i = 0; i < arr.length; i++) {
		var src = arr[i].match(srcReg);
		srcArr.push(getpicurl(src[1]));
	}
	return srcArr;
}

//正则获取p标签中的url地址
function RegularGetVideoUrlArr(str) {
	var UrlArr = new Array();
	var reg = /<p.*?class[ \t]*=[ \t]*"edui-video-resource".*?>.*?<\/p>/ig;
	//匹配url属性
	var urlReg = /url=[\'\"]?([^\'\"]*)[\'\"]?/i;
	var arr = str.match(reg);
	for (var i = 0; i < arr.length; i++) {
		var src = arr[i].match(urlReg);
		UrlArr.push((src[1]));
	}
	return UrlArr;
}

//正则获取p标签中的url地址
function RegularGetVideotwoUrlArr(str) {
	var UrlArr = new Array();

	if (str.indexOf("src") > 0 && str.indexOf("video") > 0) {

		var reg = /<source.*?(?:>|\/>)/gi;
		//匹配url属性
		var urlReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
		var arr = str.match(reg);
		for (var i = 0; i < arr.length; i++) {
			var src = arr[i].match(urlReg);
			UrlArr.push((src[1]));
		}
	} else {

	}
	return UrlArr;
}

//展示图片
function showImg(obj) {
}

//播放视频
function toPlayVideo(obj) {
	var url = obj.attributes["video-url"].nodeValue;
	if (!isEmpty(url)) {
		api.openWin({useWKWebView:true,
			name : 'BaiDuWebVideo',
			url : '../Study/BaiDuWebVideo.html',
			pageParam : {
				title : '',
				url : url,
				token : '',
			},
			slidBackEnabled : false,
			reload : true
		});
	} else {
		ShowToast('暂无视频')
	}
}

//判断当前的题目分数字段
function judgeQuestionScore(list) {
	var scoreStr = "";
	if (!isEmpty(list)) {
		if (list.hasOwnProperty('score')) {
			return list.score;
		} else if (list.hasOwnProperty('qfenshu')) {
			return list.qfenshu;
		} else {

			return ''
		}

	} else {
		return ''
	}
}

//设置考试初始化代码
function SetTestExamUI(data) {
	var questionLen = data.length;
	//	生成答题卡题号
	popoverCardNum(questionLen);
	var TestHtml = "";
	var htmlStr = "";
	for (var i = 0; i < questionLen; i++) {
		if (!isEmpty(data[i])) {
			htmlStr = GetQuestionHtml(data[i], data.length, i);
		}
		TestHtml = TestHtml + htmlStr;
	}
	return TestHtml;
}

//设置考试结果初始化代码(因为网络问题导致部分题目无法提交，但已经提交了部分数据)
function SetTestExamScoreUI(data, scoreData) {
	var questionLen = data.length;
	//	生成答题卡题号
	popoverCardNum(questionLen);
	var TestHtml = "";
	var htmlStr = "";
	for (var i = 0; i < questionLen; i++) {
		if (!isEmpty(data[i])) {
			htmlStr = GetQuestionScoreHtml(scoreData, data[i], data.length, i);
		}
		TestHtml = TestHtml + htmlStr;
	}
	return TestHtml;
}

//初始化Swipe组件
function InitmySwiper() {
	var mySwiper = new Swiper('.swiper-container', {
		autoHeight : true,
		//用户操作swiper之后，是否禁止autoplay
		autoplayDisableOnInteraction : true,
		direction : 'horizontal',
		loop : false,
	})
	// 点击答题卡切换
	$("#popover-card span").on("click", function() {
		var index = $(this).index();
		mySwiper.slideTo(index, 1000, false);
		mui('#popover-card').popover('hide')
	});
}

//得到每道题目题目ui
function GetQuestionHtml(data, len, i) {
	var qxuan = qxuanxiangIsNull(data.qxuanxiang);
	var scoreStr = (i + 1) + '/' + len + "." + "(" + judgeQuestionScore(data) + "分)";
	var questionStr = questionsImgIsNull(data.questions);
	var qutestionItemHtml = GetquestionItemStr(qxuan, data, i);
	return '<div class="swiper-slide" id="item' + (i + 1) + '" itemtype="' + data.qtype + '" qid="' + data.qid + '">' + '<div class="k-con-part3 clearfix">' + '<span id="itemType" class="k-f18">' + ByIdGetQuestionTypeName(data.qtype) + '</span>' + '<p class="k-f16 k-c323 k-mt5 indent-5">' + scoreStr + questionStr + '</p>' + GetquestionMessageStr(data.qtype) + '<div class="test-answer-list left_10"  id="question_item' + data.qid + '"  onclick="GetAnswerData(' + i + ')">' + qutestionItemHtml + '</div>' + '</div>' + '</div>';
}

//得到每道题目结果ui
function GetQuestionScoreHtml(scoreData, data, len, i) {
	var qxuan = qxuanxiangIsNull(data.qxuanxiang);
	var scoreStr = (i + 1) + '/' + len + "." + "(" + judgeQuestionScore(data) + "分)";
	var questionStr = questionsImgIsNull(data.questions);
	var qutestionScore = scoreData[data.qid];
	judgeQuestionTypeReturnAnswerCardType(qutestionScore, i)
	var qutestionItemHtml = GetquestionItemScoreStr(qutestionScore, qxuan, data, i);

	return '<div class="swiper-slide" id="item' + (i + 1) + '" itemtype="' + data.qtype + '" qid="' + data.qid + '">' + '<div class="k-con-part3 clearfix">' + '<span id="itemType" class="k-f18">' + ByIdGetQuestionTypeName(data.qtype) + '</span>' + '<p class="k-f16 k-c323 k-mt5 indent-5">' + scoreStr + questionStr + '</p>' + GetquestionMessageStr(data.qtype) + '<div class="test-answer-list left_10"  id="question_item' + data.qid + '"  onclick="GetAnswerData(' + i + ')">' + qutestionItemHtml + '</div>' + '</div>' + '</div>';
}

//得到题目选项结果html
function GetquestionItemScoreStr(qutestionScore, qxuan, data, i) {
	var questionItemHtml = "";
	var xuanzeStr = "";
	var questionItemStr = "";
	var inputStr = "";
	for (var k = 0; k < qxuan.length; k++) {
		var xuanzeStr1 = data.qtype != 3 ? qxuan[k].xuanzhe : panxuanzeReturnStr(k);
		xuanzeStr = xuanzheImgIsNull(xuanzeStr1);

		if (data.qtype == 1 || data.qtype == 3 || data.qtype == 8) {

			if ((k + 1 ) != isUpperCase(qutestionScore)) {
				inputStr = '<input  Itemindex="' + i + '" dataindex="' + (k + 1) + '" name="radio' + data.qid + '" type="radio"   itemid="' + data.qid + '">';
			} else {
				inputStr = '<input  Itemindex="' + i + '" dataindex="' + (k + 1) + '"  checked="checked"     name="radio' + data.qid + '" type="radio"   itemid="' + data.qid + '">';
			}
			questionItemStr = '<div class="mui-input-row mui-radio mui-left " >' + '<label>' + xuanzeStr + '</label>' + inputStr + '</div> '

		} else if (data.qtype == 2 || data.qtype == 7|| data.qtype == 9) {

			if (getExamMultiSelectionNumArr(qutestionScore).toString().indexOf(k + 1) == -1) {
				inputStr = '<input  Itemindex="' + i + '" dataindex="' + (k + 1) + '" name="checkbox1" value="Item 1" type="checkbox"  itemid="' + data.qid + '">'
			} else {
				inputStr = '<input  Itemindex="' + i + '" dataindex="' + (k + 1) + '" name="checkbox1" value="Item 1"  checked="checked" type="checkbox"  itemid="' + data.qid + '">'
			}
			questionItemStr = '<div class="mui-input-row mui-checkbox mui-left">' + '<label>' + xuanzeStr + '</label>' + inputStr + '</div>'
		} else if (data.qtype == 4 || data.qtype == 10) {//暂时不支持
			judgeQuestionTypeReturnAnswerCardType(qutestionScore, i, 2)
			questionItemStr = '<textarea Itemindex="' + i + '" class="k-texttarea k-f14" id="content" itemid="' + data.qid + '" disabled="disabled">' + qutestionScore + '</textarea>';
		} else if (data.qtype == 5 ) {//暂时不支持
			//		    questionItemStr="";
		}
		questionItemHtml = questionItemHtml + questionItemStr;
	}
	return questionItemHtml
}

//根据题目类型返回答题卡状态
/*type:0是单选题，1是多选题,2论述题

 * score 题目结果字段
 * */
function judgeQuestionTypeReturnAnswerCardType(score, i) {
	// if(type==0||type==2||type==1){
	if (!isEmpty(score) && score != "[]") {
		$("#scroll-card").find("span").eq(i).removeClass('qusetion-num-normal').addClass('qusetion-num-selected');
	}
	// }
}

//获取考核中多选的结果的数字形式数组
function getExamMultiSelectionNumArr(obj) {
	var keys1 = [];
	if (obj != '' && obj != null && obj != undefined) {

		if ( typeof obj == 'object') {
			for (var p1 in obj) {
				if (obj.hasOwnProperty(p1)) {
					keys1.push(isUpperCase(p1));
				}
			}
			return keys1.sort();
		} else if ( typeof obj == 'string') {
			return getLetterChangeNumberArr(obj);

		} else {
			return '';
		}
	} else {
		return '';
	}
}

//得到大写字母数组
function getLetterChangeNumberArr(list) {
	var NumberArr = new Array();
	if (!isEmpty(list)) {
		var arr = list.split("|");
		for (var i = 0; i < arr.length; i++) {
			NumberArr.push(isUpperCase(arr[i]))
		}
		return NumberArr.sort();
	} else {
		return NumberArr
	}
}

//得到题目选项html
function GetquestionItemStr(qxuan, data, i) {
	var questionItemHtml = "";
	var xuanzeStr = "";
	var questionItemStr = "";
	for (var k = 0; k < qxuan.length; k++) {
		var xuanzeStr1 = data.qtype != 3 ? qxuan[k].xuanzhe : panxuanzeReturnStr(k);
		xuanzeStr = xuanzheImgIsNull(xuanzeStr1);
		if (data.qtype == 1 || data.qtype == 3 || data.qtype == 8) {
			questionItemStr = '<div class="mui-input-row mui-radio mui-left " >' + '<label>' + xuanzeStr + '</label>' + '<input  Itemindex="' + i + '" dataindex="' + (k + 1) + '" name="radio' + data.qid + '" type="radio"   itemid="' + data.qid + '">' + '</div> '

		} else if (data.qtype == 2 || data.qtype == 7) {
			questionItemStr = '<div class="mui-input-row mui-checkbox mui-left">' + '<label>' + xuanzeStr + '</label>' + '<input  Itemindex="' + i + '" dataindex="' + (k + 1) + '" name="checkbox1" value="Item 1" type="checkbox"  itemid="' + data.qid + '">' + '</div>'
		} else if (data.qtype == 4 || data.qtype == 10) {
			questionItemStr = '<textarea Itemindex="' + i + '" class="k-texttarea k-f14" id="content" itemid="' + data.qid + '"></textarea>';
		} else if (data.qtype == 9) {
			questionItemStr = '<div class="mui-input-row mui-checkbox mui-left">' + '<label>' + xuanzeStr + '</label>' + '<input  Itemindex="' + i + '" dataindex="' + (k + 1) + '" name="checkbox1" value="Item 1" type="checkbox"  itemid="' + data.qid + '">' + '</div>';
		} else {
			questionItemStr = "";
		}
		questionItemHtml = questionItemHtml + questionItemStr;
	}
	return questionItemHtml
}

//题目附加信息
function GetquestionMessageStr(qtype) {
	var questionHtml = "";
	if (qtype == 5) {
		questionHtml = '<p class="k-cred left_10">' + "移动端暂不支持上传题，请在PC端查看！" + '</p>'
	} else if (qtype == 9) {
		questionHtml = ''
	} else {
		questionHtml = "";
	}

	return questionHtml;
}

//根据考试类型设置考试提交结果
function judgeTypeGetdtjieguoStr(data, type) {
	var Cjjieguo = '';
	switch(type) {
		case 0:
			//认证考试
			break;
		case 1:
			//模拟考试

			if (json_jieguo[data.qid] != null && json_jieguo[data.qid] != '' && json_jieguo.hasOwnProperty(data.qid) == true) {
				Cjjieguo = json_jieguo[data.qid];
			}
			break;
		case 2:
			//技能课程章节练习

			Cjjieguo = JSON.parse(json_jieguo[data.qid]);
			break;
		case 3:
			//学生端课程考试
			break;
		case 4:
			//学生端测验章节
			break;
		case 5:
			//老师端测验章节
			break;
		case 6:
			//老师端课程考试
			break;
		default:
			break;
	}
	return Cjjieguo;

}

//根据类型返回当前题目对错提示语
function JudgeGetQuestionResultStr(dtjieguoStr, qdaanStr, i, type) {
	var result = "";
	switch(type) {
		case 0:
			//认证考试
			break;
		case 1:
			//模拟考试
			result = judgeAnswerType(dtjieguoStr, qdaanStr, i);
			break;
		case 2:
			//认证考试
			result = judgeAnswerType(dtjieguoStr, qdaanStr, i);
			break;
		case 3:
			//学生端课程考试
			break;
		case 4:
			//学生端测验章节
			break;
		case 5:
			//老师端测验章节
			break;
		case 6:
			//老师端课程考试
			break;
		default:
			break;
	}
	//   alert(result);
	return result;

}

//判断是否是大写字母
function check_uppercase(obj) {
	if (/^[A-Z]+$/.test(obj.value)) {
		return true;
	} else {
		return false;
	}
}

function judgeAnswerType(CjjieguoStr, qdaanStr, i) {
	if (isEmpty(CjjieguoStr)) {//提交结果为空
		$("#scroll-card").find("span").eq(i).removeClass('qusetion-num-selected').addClass('qusetion-num-normal');
		return '<p class="k-cred">' + "您未答题,正确答案是:" + qdaanStr + '</p>'
	} else {//提交结果非空
		if (CjjieguoStr == qdaanStr) {//答案正确
			$("#scroll-card").find("span").eq(i).removeClass('qusetion-num-selected').addClass('qusetion-num-true');
			return '<p class="k-cred">' + "正确答案:" + qdaanStr + '</p>'
		} else {//答错了
			if (isEmpty(qdaanStr)) {//答案为空
				$("#scroll-card").find("span").eq(i).removeClass('qusetion-num-selected').addClass('qusetion-num-false');
				return '<p class="k-cred">' + "无正确答案" + '</p>'
			} else {
				$("#scroll-card").find("span").eq(i).removeClass('qusetion-num-selected').addClass('qusetion-num-false');
				return '<p class="k-cred">' + "您答错了,正确答案:" + qdaanStr + '</p>'
			}
		}
	}
}

//获取到提取多选题结果
function GetmultipleChoice(keys1) {
	var userchooseLetter = [];
	if (!isArrayEmpty(keys1)) {
		for (var j = 0; j < keys1.length; j++) {
			if (check_uppercase(keys1[j])) {//大写字母
				userchooseLetter.push(charCodeAt(keys1[j]))
			} else {
				userchooseLetter.push(fromCharCode(keys1[j]))
			}
		}
	}
	return userchooseLetter;
}

